package com.ironman.sailboat.mobile.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;
import java.util.Optional;

public class TSAGeoMagUtil {

    public static TSAGeoMag TSAGeoMag = new TSAGeoMag(TSAGeoMagUtil.class.getResourceAsStream("WMM.COF"));

    public static Float calDeclination(Double lat, Double lon) {
        if (Objects.isNull(lat) || Objects.isNull(lon)) {
            return null;
        }
        return BigDecimal.valueOf(TSAGeoMag.getDeclination(lat, lon)).setScale(1, RoundingMode.HALF_UP).floatValue();
    }

    public static String calDeclinationStr(Double lat, Double lon) {
        return Optional.ofNullable(calDeclination(lat, lon)).map(String::valueOf).orElse("0.0");
    }

    public static void main(String[] args) {
        System.out.println(calDeclination(23.72, 117.49)); // -4.3
        System.out.println(calDeclination(31.968518, 120.924395)); // -6.8
    }
}
